/*
 * imu_sensors.h
 *
 * Created: 8/14/2011 8:27:54 PM
 *  Author: GrubyGrub
 */ 


#ifndef IMU_SENSORS_H_
#define IMU_SENSORS_H_

#include <asf.h>
#include "marg.h"



// The avr two-wire interface uses a 7-bit number for the address, 
// and sets the last bit correctly based on reads and writes
#define ACC_ADDRESS 		(0x30 >> 1)
#define MAG_ADDRESS 		(0x3C >> 1)
#define GYRO_ADDRESS		(0xD3 >> 1)
#define ITG_ADDRESS			(0xD0 >> 1)


#define INTERNAL_ADDR_LEN		1
#define SETTINGS_DATA_LENGTH	1		
#define READ_LENGTH				6	

/*Acclermeter*/
#define CTRL_REG1_A 		(0x20)
#define CTRL_REG2_A 		(0x21)
#define CTRL_REG3_A 		(0x22)
#define CTRL_REG4_A			(0x23)
#define CTRL_REG5_A 		(0x24)
#define HP_FILTER_RESET_A 	(0x25)
#define REFERENCE_A 		(0x26)
#define STATUS_REG_A 		(0x27)

#define OUT_X_L_A  			(0x28)
#define OUT_X_H_A  			(0x29)
#define OUT_Y_L_A  			(0x2A)
#define OUT_Y_H_A  			(0x2B)
#define OUT_Z_L_A  			(0x2C)
#define OUT_Z_H_A  			(0x2D)

#define INT1_CFG_A  		(0x30)
#define INT1_SOURCE_A  		(0x31)
#define INT1_THS_A  		(0x32)
#define INT1_DURATION_A  	(0x33)
#define INT2_CFG_A  		(0x34)
#define INT2_SOURCE_A  		(0x35)
#define INT2_THS_A  		(0x36)
#define INT2_DURATION_A  	(0x37)

/*Mag*/

#define CRA_REG_M  			(0x00)
#define CRB_REG_M  			(0x01)
#define MR_REG_M  			(0x02)

#define OUT_X_H_M  			(0x03)
#define OUT_X_L_M  			(0x04)
#define OUT_Y_H_M  			(0x05)
#define OUT_Y_L_M  			(0x06)
#define OUT_Z_H_M  			(0x07)
#define OUT_Z_L_M  			(0x08)

#define SR_REG_M   			(0x09)
#define IRA_REG_M   		(0x0A)
#define IRB_REG_M   		(0x0B)
#define IRC_REG_M   		(0x0C)

/*Gyro*/

#define WHO_AM_I_G 0x0F

#define CTRL_REG1_G 0x20
#define CTRL_REG2_G 0x21
#define CTRL_REG3_G 0x22
#define CTRL_REG4_G 0x23
#define CTRL_REG5_G 0x24

#define REFERENCE_G 0x25
#define OUT_TEMP_G 0x26

#define STATUS_REG_G 0x27

#define OUT_X_L_G 0x28
#define OUT_X_H_G 0x29
#define OUT_Y_L_G 0x2A
#define OUT_Y_H_G 0x2B
#define OUT_Z_L_G 0x2C
#define OUT_Z_H_G 0x2D

#define FIFO_CTRL_REG_G 0x2Eb
#define FIFO_SRC_REG_G 0x2F

#define INT1_CFG_G 0x30
#define INT1_SRC_G 0x31
#define INT1_TSH_XH_G 0x32
#define INT1_TSH_XL_G 0x33
#define INT1_TSH_YH_G 0x34
#define INT1_TSH_YL_G 0x35
#define INT1_TSH_ZH_G 0x36
#define INT1_TSH_ZL_G 0x37
#define INT1_DURATION_G 0x38

/*ITG3200*/
#define WHO_AM_I_ITG 0x00

#define SMPLRT_DIV_ITG 0x15

#define DLPF_FS_ITG 0x16

#define INT_CFG_ITG 0x17

#define INT_STATUS_ITG 0x1A

#define TEMP_OUT_H_ITG 0x1B
#define TEMP_OUT_L_ITG 0x1C

#define GYRO_XOUT_H_ITG 0x1D
#define GYRO_XOUT_L_ITG 0x1E
#define GYRO_YOUT_H_ITG 0x1F
#define GYRO_YOUT_L_ITG 0x20
#define GYRO_ZOUT_H_ITG 0x21
#define GYRO_ZOUT_L_ITG 0x22

#define PWR_MGM_ITG 0x3E

extern void init_sensors(struct marg_sensors_t* marg_s);

extern void read_sensors(struct marg_sensors_t* marg_s);

extern void print_serial_sensor(struct marg_sensors_t* marg_s, unsigned int sample_period_ms);

extern void read_sensors_r(struct marg_sensors_t* marg_s);
#endif /* IMU_SENSORS_H_ */